package com.shenhaofeng.demo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;

import com.shenhaofeng.app.component.BaseActivity;
import com.shenhaofeng.app.component.dialog.task.AsyncTaskDialogFragment;
import com.shenhaofeng.app.component.dialog.task.DelayedLoadingDialogFragment;
import com.shenhaofeng.app.component.dialog.task.Listener;
import com.shenhaofeng.app.component.dialog.task.Task;
import com.shenhaofeng.app.utils.toast.T;
import com.shenhaofeng.demo.R;

import java.util.concurrent.TimeUnit;

import rx.Observable;


/**
 * Created by Administrator on 2016/9/22.
 */
public class AsyncLoadingActivity extends BaseActivity implements View.OnClickListener, Listener<String> {

    AsyncTaskDialogFragment<String> asyncTaskDialogFragment;

    DelayedLoadingDialogFragment<String> loadingDialogFragment;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_delayed_loading);
        asyncTaskDialogFragment = findFragment("async", AsyncTaskDialogFragment.class);
        loadingDialogFragment = findFragment("delayed", DelayedLoadingDialogFragment.class);
        asyncTaskDialogFragment.setCancelable(false);
        loadingDialogFragment.setCancelable(false);
        asyncTaskDialogFragment.setListener(this);
        loadingDialogFragment.setListener(this);
        Button button1 = (Button) findViewById(R.id.open_delayed_task);
        Button button2 = (Button) findViewById(R.id.open_async_task);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
    }




    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.open_async_task: {
                asyncTaskDialogFragment.execute(getSupportFragmentManager(), "async", new Task<String>() {
                    @Override
                    public Observable<String> run() {
                        return Observable.just("异步任务完成").delay(600, TimeUnit.MILLISECONDS);
                    }
                });
                break;
            }
            case R.id.open_delayed_task: {

                loadingDialogFragment.execute(getSupportFragmentManager(), "delayed", new Task<String>() {
                    @Override
                    public Observable<String> run() {
                        return Observable.just("延时任务完成").delay(600, TimeUnit.MILLISECONDS);
                    }
                });
                break;
            }
        }

    }

    private <F extends Fragment> F findFragment(String tag, Class<F> cls) {
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(tag);
        if (fragment == null) {
            fragment = Fragment.instantiate(this, cls.getName());
            return cls.cast(fragment);
        }
        if (fragment.getClass().equals(cls)) {
            return cls.cast(fragment);
        } else {
            throw new IllegalStateException("tag和Fragment的类型不符");
        }
    }


    @Override
    public void onResult(String result) {
        T.show(this, "result => " + result);
    }


    @Override
    public void onFailure(Throwable throwable) {
        T.show(this, "error");
        throwable.printStackTrace();
    }


    @Override
    public void onCancel() {
        T.show(this, "cancel");
    }
}
